package com.jhyj.shop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;

import com.jhyj.shop.R;

import utils.SpUtils;


/**
 * 商城首页
 * author :wbin
 */
public class GuideActivity extends Activity implements View.OnClickListener{

    private ViewPager viewpager;
    private ArrayList<View> data;
    private GuidePagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        data = new ArrayList<View>();
        adapter = new GuidePagerAdapter();
        initView();
        viewpager.setAdapter(adapter);

    }

    /**
     * 初始化数据
     */
    private void initView() {

        viewpager = (ViewPager) findViewById(R.id.viewpager);

        int ids[] = {R.mipmap.guide_01, R.mipmap.guide_02, R.mipmap.guide_03};
        //将图片封装到集合中
        for (int i = 0; i < ids.length; i++) {
            View view = View.inflate(this, R.layout.guide_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_viewpager_item);
            imageView.setBackgroundResource(ids[i]);
            //将图片加入到集合中
            data.add(view);
        }
    }

    class GuidePagerAdapter extends PagerAdapter implements View.OnClickListener {

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
             container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Button tomain = (Button) data.get(position).findViewById(R.id.btn_start_main);
            if (position == data.size() - 1) {
                tomain.setVisibility(View.VISIBLE);
                tomain.setOnClickListener(this);
            }
           container.addView(data.get(position));
            return data.get(position);
        }
        @Override
        public void onClick(View v) {
            toMain();  //进入主界面
        }

    }

    /**
     * 进入主界面
     */
    private void toMain() {

        new SpUtils(GuideActivity.this).save(SpUtils.FIRST_START, false);
        startActivity(new Intent(GuideActivity.this, MainActivity.class));
        finish();


    }

    /**
     * 当点击立即体验按钮时，设置第一次进入的标记为false，并进入主界面
     */
    @Override
    public void onClick(View v) {
        new SpUtils(this).save(SpUtils.FIRST_START, false);  //将信息保存在sp中
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}